<?php

namespace app\controller\admin;

use app\base\BaseEncryptController;
use app\model\agency\AgencyModel;
use app\model\user\UserBalanceLogModel;
use app\model\user\UserEarningsModel;
use app\model\user\UserModel;
use think\Request;

/**
 * User
 * Class User
 * @package app\controller
 */
class User extends BaseEncryptController
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = new UserModel();
    }

    /**
     * 分页列表
     * @param string $status
     * @param string $keyword
     * @param int $sex
     * @param int $page
     * @param int $limit
     * @param string $order
     * @param string $agency_id
     * @param string $is_no
     * @return \think\response\Json
     */
    public function getPage($status = '', $keyword = '', $sex = 3, $page = 1, $limit = 10, $order = "", $agency_id = '', $is_no = '')
    {
        if (empty($order)) {
            $order = "a.create_time desc";
        }
        $data = $this->model->getPageData($status, $keyword, $sex, $page, $limit, $order, $agency_id, $is_no);
        return self::returnMsg(1, [
            'totalCount' => $data['total'],
            'currentPage' => $data['current'],
            'pageList' => $data['dataList']
        ]);
    }

    /**
     * 获取推广用户列表
     * @param string $status
     * @param string $keyword
     * @param int $sex
     * @param int $page
     * @param int $limit
     * @param string $user_id
     * @return \think\response\Json
     */
    public function getPopPage($status = '', $keyword = '', $sex = 3, $page = 1, $limit = 10, $user_id = '')
    {
        $data = $this->model->getPopPageData($status, $keyword, $sex, $page, $limit, '*', $user_id);
        return self::returnMsg(1, [
            'totalCount' => $data['total'],
            'currentPage' => $data['current'],
            'pageList' => $data['dataList']
        ]);
    }

    /**
     * 单条信息
     * @param $id
     * @return \think\response\Json
     */
    public function getInfo($id)
    {
        $banner = $this->model->getInfo($id);
        if (!empty($banner)) {
            return self::returnMsg(1, $banner);
        } else {
            return self::returnMsg(1001);
        }
    }

    /**
     * 保存
     * @param Request $request
     * @return \think\response\Json
     */
    public function onSave(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'banner_title' => $request->param('banner_title'),
            'banner_src' => $request->param('banner_src'),
            'jump_type' => $request->param('jump_type'),
            'jump_link' => $request->param('jump_link'),
            'status' => $request->param('status'),
            'rank' => $request->param('rank')
        ];
        if (empty($formData['id'])) {
            $validate = 'app\validate\BannerValidate.add';
        } else {
            $validate = 'app\validate\BannerValidate';
        }
        $result = $this->validate($formData, $validate);

        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $ret = $this->model->saveData($formData, true, true);
            if (false !== $ret['flag']) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }

    /**
     * 删除
     * @param $id
     * @return \think\response\Json
     */
    public function onDelete($id)
    {
        $flag = $this->model::destroy(['id' => $id]);
        if (false !== $flag) {
            $this->model->delRedis($id);
            $this->model->redis->zRem($this->model->redis_key . ':rank', $id);
            $this->model->addToRecycle($id, 'UserModel', '用户管理', $this->currentUser['user_id'], $this->currentUser['user_type'], 'user');
            return self::returnMsg(1);
        } else {
            return self::returnMsg(1002);
        }
    }

    /**
     * 保存状态
     * @param Request $request
     * @return \think\response\Json
     */
    public function onStatus(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'status' => $request->param('status'),
        ];
        $validate = 'app\validate\UserValidate.upstatus';
        $result = $this->validate($formData, $validate);

        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $ret = $this->model->saveData($formData, false, false);
            if (false !== $ret['flag']) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }

    /**
     * 会员状态
     * @param Request $request
     * @return \think\response\Json
     */
    public function onMemberStatus(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'member_level' => $request->param('member_level'),
        ];
        $validate = 'app\validate\UserValidate.upmember';
        $result = $this->validate($formData, $validate);

        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            $formData['member_date'] = time() + (86400 * 365 * 20);
            $ret = $this->model->saveData($formData, false, false);
            if (false !== $ret['flag']) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }

    /**
     * 更改备注信息
     * @param Request $request
     * @return \think\response\Json
     */
    public function onMemberRemark(Request $request)
    {
        $formData = [
            'id' => $request->param('id'),
            'name_remark' => $request->param('name')
        ];
        $validate = 'app\validate\UserValidate.upname';
        $result = $this->validate($formData, $validate);

        if (true !== $result) {
            return self::returnMsg(991, [], $result);
        } else {
            print_r($formData);
            $ret = $this->model->saveData($formData, false, false);
            if (false !== $ret['flag']) {
                return self::returnMsg(1);
            } else {
                return self::returnMsg(0);
            }
        }
    }

    /**
     * 获取代理商列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAgencyList()
    {
        $agency_model = new AgencyModel();
        $agency_list = $agency_model->getAll([], 'id,agency_title');
        return self::returnMsg(1, $agency_list);
    }

    /**
     * 获取用户余额日志
     * @param int $page
     * @param int $limit
     * @param string $user_id
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function onBalanceLog($page = 1, $limit = 10, $user_id = '')
    {
        $user_balance_log_model = new UserBalanceLogModel();
        $map['user_id'] = $user_id;
        $order = "id DESC";
        $data = $user_balance_log_model->getPageList($map, $order, $page, $limit, $field = '*');
        return self::returnMsg(1, [
            'totalCount' => $data['total'],
            'currentPage' => $data['current'],
            'pageList' => $data['dataList']
        ]);
    }

    /**
     * 修改用户余额
     * @param Request $request
     * @return \think\response\Json
     */
    public function onChangePrice(Request $request)
    {
        $id = $request->param('id');
        $pay_price = $request->param('pay_price');
        $type = $request->param('type');
        if (empty($id) || empty($pay_price) || empty($type)) {
            return self::returnMsg(991, [], "参数有误");
        }
        if (floatval($pay_price) <= 0) {
            return self::returnMsg(991, [], "参数有误");
        }
        $ret = $this->model->changePayPrice($id, $pay_price, $type);
        return self::returnMsg($ret);
    }

    /**
     * 绑定代理商与用户的关系
     * @param $id
     * @param $aid
     * @return \think\response\Json
     */
    public function onSetAgency($id, $aid)
    {
        $ret = $this->model->where(['id' => $id])->update(['agency_id' => $aid]);
        if ($ret !== false) {
            return self::returnMsg(1);
        } else {
            return self::returnMsg(991, [], "绑定关系失败");
        }
    }

    /**
     * 解除代理商与用户的关系
     * @param $id
     * @return \think\response\Json
     */
    public function onClearAgency($id)
    {
        $ret = $this->model->clearAgency($id);
        if (true !== $ret) {
            return self::returnMsg(991, [], "解除关系失败");
        } else {
            return self::returnMsg(1);
        }
    }

    /**
     * 清除绑定关系
     * @param string $id
     * @return \think\response\Json
     */
    public function onClearBind($id = '')
    {
        if (empty($id)) {
            return self::returnMsg(991, [], "解除关系失败");
        }
        $ret = UserModel::clearBind($id);
        if ($ret) {
            return self::returnMsg(1, [], "解除关系成功");
        }
        return self::returnMsg(991, [], "解除关系失败");
    }
}
